// 入口文件

import Vue from 'vue'



// -----------------------MINT组件---------------------------------

import { Header, Swipe, SwipeItem, Button, Lazyload,Switch } from 'mint-ui';

Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem)
Vue.component(Button.name, Button);
Vue.use(Lazyload);
Vue.component(Switch.name, Switch);


// ---------------------------MUI组件---------------------------------------

import './lib/mui/css/mui.min.css'
import './lib/mui/css/icons-extra.css'


// ----------------------------地址---------------------------

import app from'./App.vue'

//------------------------------引入缩略图插件------------------
import VuePreview from 'vue-preview'
Vue.use(VuePreview)
// ------------------------引入AJAX插件--------------------

import vueResource from 'vue-resource'

Vue.use(vueResource)

Vue.http.options.root ='http://47.89.21.179:8080/'
Vue.http.options.emulateJSON = true;

// -------------------------------引入路由--------------------------

import VueRouter from 'vue-router'

Vue.use(VueRouter)

import router from './router.js'

// --------------------引入时间组件-------------------------
import moment from 'moment'


// ----------------------引入vuex------------------------------
import Vuex from 'vuex'

Vue.use(Vuex)


var localcar = JSON.parse(localStorage.getItem('car') || '[]')
const store = new Vuex.Store({
    state:{
            car: localcar     
    },
    mutations:{
         addshop(state,pathinfo){
             var flag=false
              state.car.some(item=>{
                  if(item.id===pathinfo.id){
                    item.count += parseInt(pathinfo.count)
                    flag=true
                    return true
                  }
              })
              if(flag==false){
                  state.car.push(pathinfo)
              }
             var strcar = JSON.stringify(state.car)
             localStorage.setItem('car', strcar)
         },
         updatecar(state,pathinfo){
             state.car.forEach(item=>{
                 if(item.id===pathinfo.id){
                     item.count=parseInt(pathinfo.count)
                     return true
                 }    
             })
             localStorage.setItem('car', JSON.stringify(state.car))
         },
         delcar(state,pathinfo){
             state.car.forEach((item,i)=>{
                 if(item.id==pathinfo){
                     state.car.splice(i,1)
                     return true
                 }
             })
             localStorage.setItem('car', JSON.stringify(state.car))
         },
        updateselect(state, pathinfo) {
            state.car.forEach(item => {
                if (item.id == pathinfo.id) {
                    item.selected = pathinfo.okstatus
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters:{
           getallcount(state){
              var allcount =0
              state.car.forEach(item=>{
                 allcount += item.count
               })
               return allcount
           },
           getoknum(state){
               var w ={}
              state.car.forEach(item=>{
                  w[item.id]=item.count
              })
              return w
           },

           getselect(state){
               var s = {}
               state.car.forEach(item=>{
                   s[item.id]=item.selected
               })
               return s
           },
           getprice(state){
               var all={
                   allprice:0,
                   allproduct:0
               }
               state.car.forEach(item=>{
                   if(item.selected==true){
                       all.allprice += item.count*item.price
                       all.allproduct += item.count
                   }
               })
               return all
           }
         
    }
})


// --------------------------------代码段-------------------
Vue.filter('solveTime', function (time, pattern = 'YYYY-MM-DD HH:mm:ss') {
    return moment(time).format(pattern)
})

var vm = new Vue({
    el:'#app',
    render:c => c(app),
    router,
    store
})


